/* 
 * Copyright [2018] [Alex/libo(liboms@hotmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.apidoc.scanner.help;

import java.io.File;
import java.util.function.Function;
import java.util.stream.Stream;

import com.hyts.apidoc.config.ApiDocConfig;
import com.hyts.apidoc.utility.PathUtils;
import com.hyts.apidoc.utility.StringUtils;

/**
 * @title ClassHelper.java
 * @package com.hyts.apidoc.scanner.help
 * @description TODO
 * @author libo/Alex
 * @email liboms@hotmail.com/liboware@163.com
 * @date 2018年4月20日
 * @version V1.0
 */
public final class ClassHelper {
	
	/**
	 * 基础操作类
	 */
	static Function<String,String> getBasePackage = (param) ->{
		if(StringUtils.isNotBlank(param)) {
			//肯定为基包
			if(param.indexOf(".") < 0) {
				return param;
			}else{
				//获取基包
				return param.substring(0,param.indexOf("."));
			}
		}
		return "";
	};
	
	/**  
	 * <b>filePathToClass</b>
	 * <p>文件路径直接转换为class加载器类</p>
	 * @param filePath
	 * @param packages
	 * @return
	 * @exception
	 */ 
	public static Class filePathToClass(String filePath,String[] packages) {
		//遍历基础包
		for(String packageName:packages) {
			if(filePath.contains(PathUtils.package2Path(packageName))) {
				String classPath = filePath.substring(filePath.indexOf(PathUtils.package2Path(packageName)));
				 try {
					return Class.forName(PathUtils.path2Package(classPath).
							substring(0,PathUtils.path2Package(classPath).lastIndexOf(".")));
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
					return null;
				} 
			}
		}
		return null;
    }
	
	/**  
	 * <b>TODO(方法功能的描述)</b>
	 * <p>TODO(这里用一句话描述这个方法的作用)</p>
	 * @param filePath
	 * @param packages
	 * @return
	 * @exception
	 */ 
	public static String filePathToUserPath(String filePath,String[] packages) {
		//操作用户基础路径
		filePath = filePath.replace(File.separator,"/");
		for(String packageName:packages) {
			String packageNamePath = packageName.replace(".","/");
			if(filePath.contains(packageNamePath)) {
				StringBuilder path = new StringBuilder();
				   int index = filePath.
						   indexOf(ApiDocConfig.DEFAULT_CLASS_COMPILEED_MAIN_PATH_SUBFIX)>-1?filePath.
						   indexOf(ApiDocConfig.DEFAULT_CLASS_COMPILEED_MAIN_PATH_SUBFIX):filePath.
						indexOf(ApiDocConfig.DEFAULT_CLASS_COMPILED_TEST_PATH_SUBFIX);
				   String dealFilePath = filePath.substring(0,index+1);
				   path.append(dealFilePath).
					//path.append(ApiDocConfig.DEFAULT_PROJECT_USER_PATH).
						append("/").
						append(switchCompilePath(filePath)).
						append(filePath.substring(filePath.indexOf(packageNamePath)));
				return path.toString().replace(".class",".java");
			}
		}
		return null;
    }
	
	/**  
	 * <b>选择编译后的路径</b>
	 * @param filePath
	 * @return
	 * @exception
	 */ 
	private static final String switchCompilePath(String filePath) {
	    return filePath.indexOf(ApiDocConfig.DEFAULT_CLASS_COMPILEED_MAIN_PATH_SUBFIX)>-1?ApiDocConfig.DEFAULT_PROJECT_TYPE_BASE:ApiDocConfig.DEFAULT_PROJECT_TYPE_TEST;
	}
	
	/**  
	 * <b>findBasePackage</b>
	 * <p>获取基础包</p>
	 * @param packages
	 * @return
	 * @exception
	 */ 
	public static Stream<String> findBasePackage(String[] packages) {
		//长度排序 且进行去重
		return Stream.of(packages)/*.sorted((param1, param2) -> param1.length() - param2.length())*/
				.map(getBasePackage).distinct();
	}

}
